package com.guan.ordering_sys.generator.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guan.ordering_sys.common.PageResult;
import com.guan.ordering_sys.common.StateCode;
import com.guan.ordering_sys.exception.BaseException;
import com.guan.ordering_sys.generator.entity.Post;
import com.guan.ordering_sys.generator.mapper.PostMapper;
import com.guan.ordering_sys.generator.service.PostService;
import com.guan.ordering_sys.param.PageParam;
import com.guan.ordering_sys.param.PostParam;
import com.guan.ordering_sys.vo.PostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  PostServiceImpl 服务实现类
 * </p>
 *
 * @author 关远键
 * @version v1.0.3
 * @since 2021-06-18
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {


    @Autowired
    private PostMapper postMapper;

    /**
     * 分页角色列表
     * @author 关远键
     * @date 2021/6/27 22:04
     * @param pageParam 分页参数
     * @return com.guan.ordering_sys.common.PageResult
     */
    @Override
    public PageResult getList(PageParam pageParam) {
        Page<PostVo> voPage = new Page<>();
        voPage.setCurrent(pageParam.getPageNum());
        voPage.setSize(pageParam.getPageSize());

        IPage<PostVo> list = postMapper.getList(voPage);

        if (list.getSize() == 0) {
            throw new BaseException(StateCode.NOT_FOUND);
        }

        return new PageResult(list.getRecords().size(), list.getRecords());
    }

    /**
     * 根据角色编号获取角色信息
     * @author 关远键
     * @date 2021/6/27 22:08
     * @param id
     * @return com.guan.ordering_sys.vo.PostVo
     */
    @Override
    public PostVo getById(Integer id) {
        PostVo postVo = postMapper.getById(id);
        if (postVo == null) {
            throw new BaseException(StateCode.NOT_FOUND);
        }
        return postVo;
    }

    @Override
    public Post selectById(Integer id) {
        Post post = postMapper.selectById(id);
        if (post == null) {
            throw new BaseException(StateCode.NOT_FOUND);
        }
        return post;
    }

    /**
     * 添加角色
     * @author 关远键
     * @date 2021/6/27 22:26
     * @param addParam
     * @return java.lang.Integer
     */
    @Override
    public Integer add(PostParam addParam) {
        Post post = new Post();
        post.setCompanyId(5);
        post.setPostWork("测试用的，没有用");
        post.setPostName(addParam.getName());
        post.setPostStatus(addParam.getStatus());
        post.setCreateTime(addParam.getCreateTime());

        int i = postMapper.insert(post);
        if (i > 0) {
            return post.getPostId();
        } else {
            throw new BaseException(StateCode.PARAMETER_EXCEPTION);
        }
    }

    /**
     * 根据角色编号删除角色
     * @author 关远键
     * @date 2021/6/27 22:31
     * @param id
     * @return int
     */
    @Override
    public int deleteById(Integer id) {
        if (postMapper.selectById(id) == null) {
            throw new BaseException(StateCode.NOT_FOUND);
        }

        int i = postMapper.deleteById(id);
        if (i > 0) {
            return i;
        } else {
            throw new BaseException(StateCode.DAO_EXCEPTION);
        }
    }

    /**
     * 根据角色编号更新角色信息
     * @author 关远键
     * @date 2021/6/27 22:37
     * @param id
     * @param param
     * @return int
     */
    @Override
    public int updateById(Integer id, PostParam param) {

        Post post = postMapper.selectById(id);
        post.setPostName(param.getName());
        post.setCreateTime(param.getCreateTime());
        post.setPostStatus(param.getStatus());

        int i = postMapper.updateById(post);
        if (i > 0) {
            return i;
        } else {
            throw new BaseException(StateCode.DAO_EXCEPTION);
        }
    }

}
